package interceptor;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 配置类，增加自定义过滤器
 * @author hourisheng
 * @date 2020-07
 */
public class myFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        if(req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
            HttpServletResponse response = (HttpServletResponse) res;
            HttpServletRequest request = (HttpServletRequest) req;
            if("GET".equals(request.getMethod()) && request.getRequestURL().toString().contains("/del")){
                return;
            }
            String origin=request.getHeader("Origin");
            response.setHeader("Access-Control-Allow-Origin",origin);
            response.setHeader("Access-Control-Allow-Methods","POST, GET, PUT, OPTIONS, DELETE, PATCH");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,authorization,Accept");
            //设置浏览器不缓存
            response.setHeader("Pragma","no-cache");
            response.setDateHeader("Expires",0);
            response.addHeader( "Cache-Control", "no-cache" );//浏览器和缓存服务器都不应该缓存页面信息
            response.addHeader( "Cache-Control", "no-store" );//请求和响应的信息都不应该被存储在对方的磁盘系统中；
        }
        chain.doFilter(req, res);
    }

    @Override
    public void destroy() {

    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}